package com.hs.panghu.dao.impl;

import com.hs.panghu.dao.CommunityDao;
import com.hs.panghu.entity.Community;
import com.hs.panghu.util.JDBCDruidUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CommunityDaoImpl implements CommunityDao {
    @Override
    public int insertCommunity(Community community) {
        String sql = "insert into community values(null,?,?,?,?,?,?,?,now(),?,?,?,?,?,?)";
        Object obj[] = {
          community.getCommunityName(),
          community.getCommunityProvince(),
          community.getCommunityCity(),
          community.getCommunityDistrict(),
          community.getCommunityAddress(),
          community.getCommunityLatitude(),
          community.getCommunityLongitude(),
          community.getCommunityShopCount(),
          community.getCommunityAreaCode(),
          community.getCommunityCoordinates(),
          community.getCommunityStatus(),
          community.getCommunityUserCount(),
          community.getDistance()
        };
        int row = JDBCDruidUtil.update(sql, obj);
        return row;
    }

    @Override
    public int updateCommunity(Community community) {
        String sql = "update community set communityName=?,communityProvince=?,communityCity=?,communityDistrict=?,communityAddress=?,communityLatitude=?,communityLongitude=?,createTime=now(),communityShopCount=?,communityAreaCode=?,communityCoordinates=?,communityStatus=?,communityUserCount=?,distance=? where communityId = ?";
        Object obj[] = {
                community.getCommunityName(),
                community.getCommunityProvince(),
                community.getCommunityCity(),
                community.getCommunityDistrict(),
                community.getCommunityAddress(),
                community.getCommunityLatitude(),
                community.getCommunityLongitude(),
                community.getCommunityShopCount(),
                community.getCommunityAreaCode(),
                community.getCommunityCoordinates(),
                community.getCommunityStatus(),
                community.getCommunityUserCount(),
                community.getDistance(),
                community.getCommunityId()
        };
        int row = JDBCDruidUtil.update(sql, obj);
        return row;
    }

    @Override
    public int deleteCommunity(Integer community) {
        String sql = "delete from community where communityId=?";
        Object obj[] = {community};
        int row = JDBCDruidUtil.update(sql, obj);
        return row;
    }

    @Override
    public List<Community> selectCommunity(Map<String, String> map) {
        List<Community> list = new ArrayList<>();
        //动态sql和动态数组
        StringBuffer stb = new StringBuffer("select * from community where 1=1 ");
        List param = new ArrayList();
        //判断name是否有数据，如果有，则拼接
        String name = map.get("name");
        if(name!=null && !"".equals(name)){
            stb.append(" and communityName like ? ");
            param.add("%"+name+"%");
        }
        //判断日期是否存在
        String startTime = map.get("startTime");
        String endTime = map.get("endTime");
        if(startTime!=null && !"".equals(startTime)){
            stb.append(" and createTime between ? and ? ");
            param.add(startTime);
            param.add(endTime);
        }
        //分页处理
        Integer pageNum = Integer.valueOf(map.get("pageNum"));
        Integer pageSize = Integer.valueOf(map.get("pageSize"));
        stb.append(" order by communityId desc limit ?,? ");
        param.add((pageNum-1)*pageSize);
        param.add(pageSize);
        String sql = stb.toString();
        Object object[] = param.toArray();
        System.out.println(sql);
        ResultSet resultSet = JDBCDruidUtil.query(sql, object);
        try {
            while (resultSet.next()){
                Community community = new Community(
                        resultSet.getInt(1),
                        resultSet.getString(2),
                        resultSet.getString(3),
                        resultSet.getString(4),
                        resultSet.getString(5),
                        resultSet.getString(6),
                        resultSet.getString(7),
                        resultSet.getString(8),
                        resultSet.getDate(9),
                        resultSet.getInt(10),
                        resultSet.getInt(11),
                        resultSet.getString(12),
                        resultSet.getInt(13),
                        resultSet.getInt(14),
                        resultSet.getString(15)
                );
                list.add(community);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            JDBCDruidUtil.close();
        }
        return list;
    }

    @Override
    public int selectCommunityCount(Map<String, String> map) {
        int count = 0;
        //动态sql和动态数组
        StringBuffer stb = new StringBuffer("select count(1) from community where 1=1 ");
        List param = new ArrayList();
        //判断name是否有数据，如果有，则拼接
        String name = map.get("name");
        if(name!=null && !"".equals(name)){
            stb.append(" and communityName like ? ");
            param.add("%"+name+"%");
        }
        //判断日期是否存在
        String startTime = map.get("startTime");
        String endTime = map.get("endTime");
        if(startTime!=null && !"".equals(startTime)){
            stb.append(" and createTime between ? and ? ");
            param.add(startTime);
            param.add(endTime);
        }
        String sql = stb.toString();
        Object object[] = param.toArray();
        System.out.println(sql);
        ResultSet resultSet = JDBCDruidUtil.query(sql, object);
        try {
            if (resultSet.next()){
                count = resultSet.getInt(1);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            JDBCDruidUtil.close();
        }
        return count;
    }
}
